package com.example.wsappproject.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.wsappproject.R;
import com.example.wsappproject.practice.ArticleEntity;

import java.util.List;

/**
 * Created by Leo on 2020/9/17.
 */

public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ArticleHolder> {

    private List<ArticleEntity.DataBean.DatasBean> mList;
    private Context mContext;

    public ArticleAdapter(
            List<ArticleEntity.DataBean.DatasBean> mList,
            Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    /**
     * 创建ViewHolder
     *
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public ArticleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(
                R.layout.item_article_article_adapter, parent, false);
        return new ArticleHolder(inflate);
    }

    /**
     * 设置数据
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(final ArticleHolder holder, int position) {
        final ArticleEntity.DataBean.DatasBean datasBean = mList.get(position);
        String superChapterName = datasBean.getSuperChapterName();
        String title = datasBean.getTitle();
        holder.tvSuperName.setText(superChapterName);
        holder.tvTitle.setText(title);

        //需要添加mListener不为空判断，
        //因为不判断mListener是否为null就使用，有可能导致空指针异常
        if (mListener != null) {
            //holder.itemView就是自己在onCreateViewHolder里面创建的视图view，
            //也就是条目视图
            //holder.itemView


            holder.myItemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //匿名内部类访问局部变量，局部变量需要设置为final
                    mListener.onArticleItemClick(
                            holder.getAdapterPosition(),
                            datasBean);
                }
            });
        }
    }

    /**
     * 获取条目数量
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ArticleHolder extends RecyclerView.ViewHolder {

        private View myItemView;
        private TextView tvSuperName;
        private TextView tvTitle;

        public ArticleHolder(View itemView) {
            super(itemView);
            this.myItemView = itemView;
            tvSuperName = itemView.findViewById(R.id.tv_super_chapter_name_item);
            tvTitle = itemView.findViewById(R.id.tv_title_item);
        }
    }

    private onArticleItemClickListener mListener;

    public void setOnArticleItemClickListener(onArticleItemClickListener listener){
        this.mListener = listener;
    }

    /**
     * 接口是类的一种形式
     * 定义的方法，不可以有方法体
     * Interface methods cannot have body
     *
     * 首页文章条目点击监听器
     */
    public interface onArticleItemClickListener{
        /**
         * 首页文章条目点击会执行的方法
         *
         * @param position 点击的条目位置
         * @param datasBean 点击条目位置的 数据对象
         */
        void onArticleItemClick(int position,
                                ArticleEntity.DataBean.DatasBean datasBean);
    }
}